package com.e.Adapter;



import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.e.Dao.UserDao;
import com.e.Pojo.Order;
import com.e.Pojo.Tickes;
import com.e.Pojo.User;
import com.e.SQL.UserDBHepler;
import com.e.ticket.MainActivity;
import com.e.ticket.R;
import com.e.ticket.Tickes_Item;
import com.e.ticket.Update_password;

import org.w3c.dom.Text;

import java.util.List;

public class TickeAdapter extends BaseAdapter {

    private List<Tickes> tickesList;
    private Context ctr;
    private LayoutInflater inflater;
    private String sno;
    private UserDao userDao;
    private User user;
    private Order order;

    public TickeAdapter(Context context,List<Tickes> list,String no){
        ctr = context;
        this.tickesList = list;
        inflater = LayoutInflater.from(context);
        sno=no;
    }

    @Override
    public int getCount() {
        return tickesList.size();
    }

    @Override
    public Object getItem(int i) {
        return tickesList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //bt_yuyue  = (Button)view.findViewById(R.id.yuyue);
        //  获取账密码

        Tickes tickes = tickesList.get(i);
        ViewHolder viewHolder = null;
        if (view == null){
            view = inflater.inflate(R.layout.list_item,viewGroup,false);
            viewHolder = new ViewHolder();
            viewHolder.start =(TextView) view.findViewById(R.id.start);
            viewHolder.end = (TextView) view.findViewById(R.id.end);
            viewHolder.time = (TextView) view.findViewById(R.id.times);
            viewHolder.btn_yuyue = (Button)view.findViewById(R.id.yuyue);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        Log.e("TAG", "getView: "+tickes.getStart() );
        viewHolder.start.setText(tickes.getStart());
        viewHolder.end.setText(tickes.getEnd());
        viewHolder.time.setText(tickes.getTime());
        viewHolder.btn_yuyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userDao = new UserDao(new UserDBHepler(view.getContext()));
                if(userDao.findCheckOrder(sno,tickes.getStart(),tickes.getTime())){
                    user = userDao.findUserBySno(sno);
                    order = new Order();
                    order.setSno(user.getSno());
                    order.setSname(user.getSname());
                    order.setDate1(tickes.getTime());
                    order.setStart(tickes.getStart());
                    order.setLast(tickes.getEnd());
                    order.setPhone(user.getPhone());
                    AlertDialog dialog = new Builder(ctr)
                            .setTitle("预约确定")
                            .setMessage("是否预约时间为:"+tickes.getTime()+"    "+tickes.getStart()+"----->"+tickes.getEnd()+"的车次")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                    System.out.println("================================================================");
                                    if(userDao.insertOrder(order)) {
                                        Toast.makeText(view.getContext(), "预约成功", Toast.LENGTH_SHORT).show();
                                        notifyDataSetChanged();
                                        Intent intent = new Intent(ctr, MainActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putInt("ids",2);
                                        intent.putExtras(bundle);
                                        ctr.startActivity(intent);

                                    }
                                    else
                                        Toast.makeText(view.getContext(),"预约失败",Toast.LENGTH_SHORT).show();
                                }
                            })
                            .create();
                    dialog.show();
                }else{
                    Toast.makeText(view.getContext(),"不能重复预约",Toast.LENGTH_SHORT).show();
                }



            }
        });
        Log.d("sno", "getView: "+sno);
        return view;
    }




    static class ViewHolder{
        public TextView start;
        public TextView end;
        public TextView time;
        public Button btn_yuyue;
    }

}
